<?php
/**
 * Scripts and stylesheets
 *
 * Enqueue stylesheets in the following order:
 * 1. /theme/assets/css/main.css
 *
 * Enqueue scripts in the following order:
 * 1. jquery-1.11.1.min.js via Google CDN
 * 2. /theme/assets/js/vendor/modernizr.min.js
 * 3. /theme/assets/js/scripts.js (in footer)
 *
 * Google Analytics is loaded after enqueued scripts if:
 * - An ID has been defined in config.php
 * - You're not logged in as an administrator
 */
function apollo_scripts() {
  /**
   * The build task in Grunt renames production assets with a hash
   * Read the asset names from assets-manifest.json
   */
    if (WP_ENV === 'development') {
        $assets = array(
            'css'                   => '/assets/css/style.css',
            'jquery'                => '/assets/js/libs.js',
            'apollo-js-plugins'     => '/assets/js/plugins.js',
            'apollo-js-start'       => '/assets/js/start.js',
        );
    } else {
        $get_assets = file_get_contents(get_template_directory() . '/assets/manifest.json');
        $assets     = json_decode($get_assets, true);
        $assets     = array(
            'css'   => '/assets/css/main.min.css?' . $assets['assets/css/main.min.css']['hash'],
            'js'    => '/assets/js/scripts.min.js?' . $assets['assets/js/scripts.min.js']['hash'],
        );
    }
    
    wp_enqueue_style('apollo_css', get_template_directory_uri() . $assets['css'], false, null);
    
//    // Add override css
//    if ( of_get_option( 'apollo_activate_override_css' ) && file_exists(get_override_css_abs_path() ) ) {
//        wp_enqueue_style('apollo_override_css', get_override_css_url(), false, null);
//    }
//    
//    if ( file_exists(get_primary_color_css_abs_path() ) ) {
//        wp_enqueue_style('apollo_primary_color_css', get_primary_color_css_url(), false, null);
//    }
    
   
    /**
     * jQuery is loaded using the same method from HTML5 Boilerplate:
     * Grab Google CDN's latest jQuery with a protocol relative URL; fallback to local if offline
     * It's kept in the header instead of footer to avoid conflicts with plugins.
     */
    if (!is_admin()) {
      wp_deregister_script('jquery');
      wp_register_script('jquery', get_stylesheet_directory_uri().$assets['jquery'], array(), null, false);
    }

    /* enque other javascript */
      wp_enqueue_script('apollo-js-plugins', get_stylesheet_directory_uri().$assets['apollo-js-plugins'], array('jquery'), null, true);
      wp_enqueue_script('apollo-js-start', get_stylesheet_directory_uri().$assets['apollo-js-start'], array('jquery','apollo-js-plugins'), null, true);

    if (is_single() && comments_open() && get_option('thread_comments')) {
      wp_enqueue_script('comment-reply');
    }

}
add_action('wp_enqueue_scripts', 'apollo_scripts', 100);


/**
 * Google Analytics snippet from HTML5 Boilerplate
 * 
 * Cookie domain is 'auto' configured. See: http://goo.gl/VUCHKM
 */
function apollo_google_analytics() { ?>
<script>
  <?php if (WP_ENV === 'production') : ?>
    (function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
    function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
    e=o.createElement(i);r=o.getElementsByTagName(i)[0];
    e.src='//www.google-analytics.com/analytics.js';
    r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
  <?php else : ?>
    function ga() {
      console.log('GoogleAnalytics: ' + [].slice.call(arguments));
    }
  <?php endif; ?>
  ga('create','<?php echo GOOGLE_ANALYTICS_ID; ?>','auto');ga('send','pageview');
</script>

<?php }
if (GOOGLE_ANALYTICS_ID && (WP_ENV !== 'production' || !current_user_can('manage_options'))) {
  add_action('wp_footer', 'apollo_google_analytics', 20);
}
